package com.readbook.service;

import com.alibaba.fastjson.JSONArray;
import com.readbook.dao.StudentDao;
import com.readbook.dao.UaccountDao;
import com.readbook.util.getthirdInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UaccountService {
    @Autowired
    private UaccountDao uaccountDao;
    @Autowired
    private StudentDao studentDao;
    public List<Map<String,Object>>Search_uaccount(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=uaccountDao.Search_uaccount(gmap);
        return list1;
    }

    //刷新账号
    public Map<String,Object> refreshUaccount()
    {
        Map<String, Object> returnmap = new HashMap<String, Object>();
        String msg="";
        //刷新学生账号
        boolean stu=false;
        stu=refreshStuUaccount();
        if(stu){
            msg+="学生数据更新成功，";
        }else{
            msg+="学生数据更新失败，";
        }

        //刷新教师账号
        boolean tea=false;
        tea=refreshTeaUaccount();
        if(tea){
            msg+="教师数据更新成功";
        }else{
            msg+="教师数据更新失败";
        }

        if(stu&&tea){
            returnmap.put("msg","更新成功");
        }else{
            returnmap.put("msg",msg);
        }
        returnmap.put("result",stu&&tea);
        return returnmap;
    }

    //刷新学生账号
    public boolean refreshStuUaccount()
    {
        //刷新学生账号
        //查询是否有学生账号数据
        List<Map<String,Object>>listu=uaccountDao.getStuUaccount();
        if(listu.size()>0){
            String lasttime=listu.get(0).get("LASTTIME").toString();
            //long last_time=Long.parseLong(lasttime);
            Map<String, Object> memmaps= getthirdInfo.getMemberList(lasttime);

            String lasttime1=memmaps.get("lasttime").toString();
            String studatas=memmaps.get("studatas").toString();
            List<Map<String,Object>> listmap = (List<Map<String,Object>>) JSONArray.parse(studatas);

            for(int i=0;i<listmap.size();i++){
                //查询学生是否已经存在，存在则修改，不存在则插入
                Map<String, Object> stumap =listmap.get(i);
                Map<String, Object> getmap=new HashMap<>();
                getmap.put("name",stumap.get("name"));
                getmap.put("sex",stumap.get("sex"));
                getmap.put("dlzh",stumap.get("phone"));
                if(stumap.get("deleted_at")!=null){
                    getmap.put("jlscsj",stumap.get("deleted_at"));
                    getmap.put("jlsczt","1");
                }
                if(stumap.get("year")!=null && !stumap.get("year").toString().equals("")){
                    String ruxn=stumap.get("year").toString();
                    Calendar cal = Calendar.getInstance();
                    int nowyear = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH )+1;
                    int nj=nowyear-Integer.parseInt(ruxn);
                    if(month>=8){
                        nj=nj+1;//因为下半学期升学
                    }
                    getmap.put("nj",nj);
                }
                getmap.put("year",stumap.get("year"));
                getmap.put("bj",stumap.get("banji_num"));
                getmap.put("zw",stumap.get("member_post_name"));
                getmap.put("lasttime",lasttime1);
                if(stumap.get("phone")!=null){
                    String phone=stumap.get("phone").toString();
                    List<Map<String,Object>>student=uaccountDao.getStuUaccountByzh(phone);
                    int m=0;
                    if(student.size()>0){
                        //修改
                        m=uaccountDao.updateUaccountByStuzh(getmap);
                    }else{
                        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                        //插入
                        getmap.put("usid",uuid);
                        getmap.put("wstate","1");
                        getmap.put("dlma","123456");
                        m=uaccountDao.insertUaccount(getmap);
                    }
                    if(m>0){
                        continue;
                    }else{
                        return false;
                    }
                }
            }
            //修改最后更新时间
            if(listmap.size()>0){
                uaccountDao.updateUaccountLastime(lasttime1);
            }
        }else{
            String last_time="0";
            Map<String, Object> memmaps= getthirdInfo.getMemberList(last_time);
            String lasttime1=memmaps.get("lasttime").toString();
            String studatas=memmaps.get("studatas").toString();
            List<Map<String,Object>> listmap = (List<Map<String,Object>>) JSONArray.parse(studatas);

            for(int i=0;i<listmap.size();i++){
                //查询学生是否已经存在，存在则修改，不存在则插入
                Map<String, Object> stumap =listmap.get(i);
                Map<String, Object> getmap=new HashMap<>();
                getmap.put("name",stumap.get("name"));
                getmap.put("sex",stumap.get("sex"));
                getmap.put("dlzh",stumap.get("phone"));
                if(stumap.get("deleted_at")!=null){
                    getmap.put("jlscsj",stumap.get("deleted_at"));
                    getmap.put("jlsczt","1");
                }
                if(stumap.get("year")!=null && !stumap.get("year").toString().equals("")){
                    String ruxn=stumap.get("year").toString();
                    Calendar cal = Calendar.getInstance();
                    int nowyear = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH )+1;
                    int nj=nowyear-Integer.parseInt(ruxn);
                    if(month>=8){
                        nj=nj+1;//因为下半学期升学
                    }
                    getmap.put("nj",nj);
                }
                getmap.put("year",stumap.get("year"));
                getmap.put("bj",stumap.get("banji_num"));
                getmap.put("zw",stumap.get("member_post_name"));
                getmap.put("lasttime",lasttime1);
                if(stumap.get("phone")!=null){
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    getmap.put("usid",uuid);
                    getmap.put("wstate","1");
                    getmap.put("dlma","123456");
                    int m=uaccountDao.insertUaccount(getmap);
                    if(m>0){
                        continue;
                    }else{
                        return false;
                    }
                }
            }
        }
        return true;
    }



    //刷新教师账号
    public boolean refreshTeaUaccount()
    {
        //刷新教师账号
        //查询是否有教师账号数据
        List<Map<String,Object>>listea=uaccountDao.getTeaUaccount();
        if(listea.size()>0){
            String lasttime=listea.get(0).get("LASTTIME").toString();
            long last_time=Long.parseLong(lasttime);

            Map<String, Object> memmaps= getthirdInfo.getteaList(last_time);
            String lasttime1=memmaps.get("lasttime").toString();
            String teadatas=memmaps.get("teadatas").toString();
            List<Map<String,Object>> listmap = (List<Map<String,Object>>) JSONArray.parse(teadatas);

            for(int i=0;i<listmap.size();i++){
                //查询教师是否已经存在，存在则修改，不存在则插入
                Map<String, Object> teamap =listmap.get(i);
                Map<String, Object> getmap=new HashMap<>();
                getmap.put("name",teamap.get("name"));
                getmap.put("sex",teamap.get("sex"));
                getmap.put("dlzh",teamap.get("phone"));
                if(teamap.get("deleted_at")!=null){
                    getmap.put("jlscsj",teamap.get("deleted_at"));
                    getmap.put("jlsczt","1");
                }
                if(teamap.get("year")!=null && !teamap.get("year").toString().equals("")){
                    String ruxn=teamap.get("year").toString();
                    Calendar cal = Calendar.getInstance();
                    int nowyear = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH )+1;
                    int nj=nowyear-Integer.parseInt(ruxn);
                    if(month>=8){
                        nj=nj+1;//因为下半学期升学
                    }
                    getmap.put("nj",nj);
                }
                getmap.put("year",teamap.get("year"));
                getmap.put("bj",teamap.get("banji_num"));
                getmap.put("zw",teamap.get("identity_name"));
                getmap.put("lasttime",lasttime1);

                if(teamap.get("phone")!=null){
                    String phone=teamap.get("phone").toString();
                    List<Map<String,Object>>teacher=uaccountDao.getTeaUaccountByzh(phone);
                    int m=0;
                    if(teacher.size()>0){
                        //修改
                        m=uaccountDao.updateUaccountByTeazh(getmap);
                    }else{
                        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                        //插入
                        getmap.put("usid",uuid);
                        getmap.put("wstate","2");
                        m=uaccountDao.insertUaccount(getmap);
                    }
                    if(m>0){
                        continue;
                    }else{
                        return false;
                    }
                }
            }
            //修改最后更新时间
            if(listmap.size()>0){
                uaccountDao.updateUaccountLastime(lasttime1);
            }
        }else{
            long last_time=0;

            Map<String, Object> memmaps= getthirdInfo.getteaList(last_time);
            String lasttime1=memmaps.get("lasttime").toString();
            String teadatas=memmaps.get("teadatas").toString();
            List<Map<String,Object>> listmap = (List<Map<String,Object>>) JSONArray.parse(teadatas);

            for(int i=0;i<listmap.size();i++){
                //查询教师是否已经存在，存在则修改，不存在则插入
                Map<String, Object> teamap =listmap.get(i);
                Map<String, Object> getmap=new HashMap<>();
                getmap.put("name",teamap.get("name"));
                getmap.put("sex",teamap.get("sex"));
                getmap.put("dlzh",teamap.get("phone"));
                if(teamap.get("deleted_at")!=null){
                    getmap.put("jlscsj",teamap.get("deleted_at"));
                    getmap.put("jlsczt","1");
                }
                if(teamap.get("year")!=null && !teamap.get("year").toString().equals("")){
                    String ruxn=teamap.get("year").toString();
                    Calendar cal = Calendar.getInstance();
                    int nowyear = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH )+1;
                    int nj=nowyear-Integer.parseInt(ruxn);
                    if(month>=8){
                        nj=nj+1;//因为下半学期升学
                    }
                    getmap.put("nj",nj);
                }
                getmap.put("year",teamap.get("year"));
                getmap.put("bj",teamap.get("banji_num"));
                getmap.put("zw",teamap.get("identity_name"));
                getmap.put("lasttime",lasttime1);
                if(teamap.get("phone")!=null){
                    int m=0;
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    //插入
                    getmap.put("usid",uuid);
                    getmap.put("wstate","2");
                    m=uaccountDao.insertUaccount(getmap);

                    if(m>0){
                        continue;
                    }else{
                        return false;
                    }
                }
            }
        }
        return true;
    }



    public Map<String,Object> Insert_uaccount(Map<String,Object>gmap)
    {
        //查询是否重复添加
        Map<String,Object>smap=new HashMap<>();
        smap.put("dlzh",gmap.get("dlzh"));
        smap.put("wstate",gmap.get("wstate"));
        List<Map<String,Object>>list1=uaccountDao.Search_uaccount(smap);
        if(list1.size()>0){
            smap.put("result",false);
            smap.put("msg","该账号已经存在，不能重复添加！");
            return smap;
        }else{
            int i=uaccountDao.insertUaccount(gmap);
            if(i>0){
                smap.put("result",true);
                smap.put("msg","添加成功！");
            }else{
                smap.put("result",false);
                smap.put("msg","添加失败！");
            }
            return smap;
        }
    }


    public Map<String,Object> Edit_uaccount(Map<String,Object>gmap)
    {
        //查询是否重复添加
        Map<String,Object>smap=new HashMap<>();
        smap.put("dlzh",gmap.get("dlzh"));
        smap.put("wstate",gmap.get("wstate"));
        String usid=gmap.get("usid").toString();
        List<Map<String,Object>>list1=uaccountDao.Search_uaccount(smap);
        for(int i=0;i<list1.size();i++){
            Map<String,Object>getm=list1.get(i);
            String usid1=getm.get("USID").toString();
            if(!usid.equals(usid1)){
                smap.put("result",false);
                smap.put("msg","该账号已经存在！");
                return smap;
            }
        }
        int i=uaccountDao.updateUaccount(gmap);
        if(i>0){
            smap.put("result",true);
            smap.put("msg","修改成功！");
        }else{
            smap.put("result",false);
            smap.put("msg","修改失败！");
        }
        return smap;
    }

    public Map<String,Object> del_uaccount(String usid)
    {
        Map<String,Object>gmap=new HashMap<>();
        gmap.put("jlsczt","1");
        gmap.put("usid",usid);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("jlscsj", df.format(new Date()));
        int i=uaccountDao.updateUaccount(gmap);
        Map<String,Object>smap=new HashMap<>();
        if(i>0){
            smap.put("result",true);
            smap.put("msg","修改成功！");
        }else{
            smap.put("result",false);
            smap.put("msg","修改失败！");
        }
        return smap;

    }
    //升级
    public Map<String,Object> upgrade_uaccount()
    {
        Map<String,Object>smap=new HashMap<>();
        //1.将所有6年级学生删除
        int m=uaccountDao.updateGradeSix();
        //2.将所有非6年级学生升级
        if(m>0){
            int i=uaccountDao.updateGradeNoSix();
            if(i>0){
                smap.put("result",true);
                smap.put("msg","升级成功！");
            }else{
                smap.put("result",false);
                smap.put("msg","升级失败！");
            }
        }else{
            smap.put("result",false);
            smap.put("msg","升级失败！");
        }
        return smap;

    }



}
